package arbor.mining.util.date;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Properties;

import arbor.mining.util.UtilItem;

/**
 * DateUtil contains many simple date utilities. Since most functions
 * are derivative from java.util.Date, we set them as static functions.
 * The properties is generally ignored in this class. 
 * @author ktchuang
 *
 */
public class DateUtil extends UtilItem{
	/**
	 * return the date object corresponding to the time in the long format.
	 * @param time
	 * @return
	 */
	public static Date getDateFromLong(long time) {
		return new Date(time);		
	}

	/**
	 * return a time with the specified "HH:mm" of today.
	 * @param time
	 * @return
	 */
	public static long getTimeOfToday(String time) {
		String[] t = time.split(":");
		GregorianCalendar cal = new GregorianCalendar();
		cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(t[0]));
		cal.set(Calendar.MINUTE, Integer.parseInt(t[1]));
		return cal.getTimeInMillis();
	}
  
	/**
	 * Return the date information of today;
	 * in the format of yyyyMMdd.
	 * @param type
	 * @return
	 */
	public static String getCurrentDateStr() {
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
		return df.format(GregorianCalendar.getInstance().getTime());
	}
	
	public static String getLogStyleTime(Date date) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy.MM.dd-HH:mm:ss");
		return df.format(date);
	}
  
	/**
	 * Return the date information of today;
	 * @return
	 */
	public static Date getCurrentDate() {		
		return GregorianCalendar.getInstance().getTime();
	}
  
	/**
	 * Return the date after the given days.
	 * @param day
	 * @return
	 */
	public static Date getDaysAfterToday(int days) {
		GregorianCalendar cal = new GregorianCalendar();
		int date = cal.get(Calendar.DAY_OF_MONTH)+days;
		cal.set(Calendar.DAY_OF_MONTH, date);
		return cal.getTime();		
	}
  
	/**
	 * Return the date after the given hours.
	 * @param hours
	 * @return
	 */
	public static Date getHoursAfterNow(int hours) {
		GregorianCalendar cal = new GregorianCalendar();
		int date = cal.get(Calendar.HOUR_OF_DAY)+hours;
		cal.set(Calendar.HOUR_OF_DAY, date);
		return cal.getTime();		
	}
  
	/**
	 * Return the date after the given minutes.
	 * @param hours
	 * @return
	 */
	public static Date getMinutesAfterNow(int minutes) {
		GregorianCalendar cal = new GregorianCalendar();
		int date = cal.get(Calendar.MINUTE)+minutes;
		cal.set(Calendar.MINUTE, date);
		return cal.getTime();		
	}
  
	/**
	 * Return current date with ":" symbol.
	 * @return
	 */
	public static String getCurrentColonDate() {
		Calendar calendar = new GregorianCalendar();
		SimpleDateFormat df = new SimpleDateFormat("yyyy:MM:dd");
		return df.format(calendar.getTime());
	}
  
  /**
   * Return current date with "-" symbol.
   * @return
   */
	public static String getCurrentDashedDate() {
		Calendar calendar = new GregorianCalendar();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		return df.format(calendar.getTime());
	}
    
	/**
	 * Return current date with "/" symbol.
	 * @return
	 */
	public static String getCurrentSlashDate() {
		Calendar calendar = new GregorianCalendar();
		SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
		return df.format(calendar.getTime());
	}
  
  /**
   * Get Chinese date with "/" symbol.
   * @return
   */
	public static String getChineseCurrentSlashDate() {
		Calendar calendar = new GregorianCalendar();
		SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
		String year = Integer.toString(calendar.get(Calendar.YEAR) - 1911);
		String md = (df.format(calendar.getTime())).substring(4, 10);
		return year + md;
	}
  
  /**
   * Change the date formate from yyyyMMdd to yyyy/MM/dd.
   * @param date
   * @return
   */
	public static String getSDateFromGDate(String date) {
		SimpleDateFormat df_o = new SimpleDateFormat("yyyyMMdd");
		SimpleDateFormat df_n = new SimpleDateFormat("yyyy/MM/dd");
		Date s = null;

		try {
			s = df_o.parse(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return df_n.format(s);
	}
  
  /**
   * Change the date formate from yyyy-MM-dd to yyyyMMdd.
   * @param date
   * @return
   */
	public static String getGDateFromDDate(String date) {
		SimpleDateFormat df_o = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat df_n = new SimpleDateFormat("yyyyMMdd");
		Date s = null;

		try {
			s = df_o.parse(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return df_n.format(s);
	}
  
  /**
   * Change the date formate from yyyy/MM/dd to yyyyMMdd.
   * @param date
   * @return
   */
	public static String getGDateFromSDate(String date) {
		SimpleDateFormat df_o = new SimpleDateFormat("yyyy/MM/dd");
		SimpleDateFormat df_n = new SimpleDateFormat("yyyyMMdd");
		Date s = null;

		try {
			s = df_o.parse(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return df_n.format(s);
	}
  
  /**
   * Change the date formate from yyyy-MM-dd to yyyy/MM/dd.
   * @param date
   * @return
   */
	public static String getSDateFromDDate(String date) {
		SimpleDateFormat df_o = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat df_n = new SimpleDateFormat("yyyy/MM/dd");
		Date s = null;

		try {
			s = df_o.parse(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return df_n.format(s);
	}
  
  /**
   * Return the list of dates in the given month. All in yyyyMMdd format.
   * @param today
   * @return
   */
	public static String[] getThisMonth(String date) {
		String[] r = new String[2];
		int[] month = new int[] {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,	31};
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
		try {
			Date s = df.parse(date);
			GregorianCalendar cal = new GregorianCalendar();
			cal.setTime(s);
			cal.add(Calendar.DATE, -cal.get(Calendar.DAY_OF_MONTH) + 1);
			r[0] = df.format(cal.getTime());
			System.out.println(r[0]);
			cal.add(Calendar.DATE, month[cal.get(Calendar.MONTH)] - 1);
			r[1] = df.format(cal.getTime());
			System.out.println(r[1]);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return r;
	}
  
  /**
   * Return the list of dates in the given week. All in yyyyMMdd format.
   * @param today
   * @return
   */
	public static String[] getThisWeek(String date) {
		String[] r = new String[2];
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
		try {
			Date s = df.parse(date);
			GregorianCalendar cal = new GregorianCalendar();
			cal.setTime(s);
			cal.add(Calendar.DATE, -cal.get(Calendar.DAY_OF_WEEK) + 2);
			r[0] = df.format(cal.getTime());
			cal.add(Calendar.DATE, 6);
			r[1] = df.format(cal.getTime());

		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return r;
	}
  
  /**
   * Check if the day is weekend when the input is of yyyyMMdd format.
   * @param today
   * @return
   */
	public static boolean isWeekend(String date) {
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
		try {
			Date s = df.parse(date);
			GregorianCalendar cal = new GregorianCalendar();
			cal.setTime(s);
			if ((cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY)
					|| (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)) {
				return true;
			}
		} catch (Exception e) {
			System.out.println(e);
		}
		return false;
	}

	/**
	 * Return the date list consisting of days between start and end, and all
	 * are of the yyyyMMdd format.
	 * @param start
	 * @param end
	 * @return
	 */
	public static String[] getDateBetween(String start, String end) {
		String[] dn = null;
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
		try {
			Date s = df.parse(start);
			Date e = df.parse(end);
			int day = (int) ((e.getTime() - s.getTime()) / (1000 * 60 * 60 * 24));
			dn = new String[day];
			GregorianCalendar cal = new GregorianCalendar();
			cal.setTime(s);
			for (int i = 0; i < dn.length; i++) {
				cal.add(Calendar.DATE, 1);
				dn[i] = df.format(cal.getTime());
				//System.out.println(dn[i]);
			}

		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return dn;
	}
  
  /**
   * Get chinese date between start and end, all of the yyyy/MM/dd format.
   * @param start
   * @param end
   * @return
   */
	public static String[] getChineseDateBetween(String start, String end) {
		String[] dn = null;
		SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
		try {
			Date s = df.parse(start);
			Date e = df.parse(end);
			int day = (int) ((e.getTime() - s.getTime()) / (1000 * 60 * 60 * 24));
			dn = new String[day];
			GregorianCalendar cal = new GregorianCalendar();
			cal.setTime(s);
			for (int i = 0; i < dn.length; i++) {
				cal.add(Calendar.DATE, 1);

				//dn[i]=df.format(cal.getTime());
				String year = Integer.toString(cal.get(Calendar.YEAR) - 1911);
				String md = (df.format(cal.getTime())).substring(4, 10);
				dn[i] = year + md;
				//System.out.println(dn[i]);
			}

		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return dn;
	}	
	
	
	@Override
	public Properties getInitProperty() {
		logger.error("The function is invalid in the class\n");
		return null;
	}
}
